local function kaiguan(inst)
    if not inst:HasTag("playerghost") and inst.components.inventory then
        local x, y, z = inst:GetPosition():Get()

        local ents = TheSim:FindEntities(x, 0, z, 2, { "_inventoryitem" },
            { "FX", "NOCLICK", "DECOR", "INLIMBO", "trap", "light", "blowdart", "projectile", '_cantbepicked' })
        for k, v in pairs(ents) do
            if v.components.inventoryitem and v.components.inventoryitem and v.components.inventoryitem.canbepickedup and v.components.inventoryitem.cangoincontainer and not v.components.inventoryitem:IsHeld() and inst.components.inventory:CanAcceptCount(v, 1) > 0 then
                inst.components.inventory:GiveItem(v, nil, v:GetPosition())
            end
        end
    end
end
AddModRPCHandler('mymod', "pickitems", function(inst)
    if not GLOBAL.TheWorld.ismastersim or not inst or not inst:IsValid() or inst:HasTag("playerghost") then
        return
    end
    if inst.kaiguan == nil then
        inst.kaiguan = inst:DoPeriodicTask(3, kaiguan)
        if inst.components.talker then
            inst.components.talker:Say("自动拾取")
        end
    elseif inst.kaiguan ~= nil then
        inst.kaiguan:Cancel()
        inst.kaiguan = nil
        if inst.components.talker then
            inst.components.talker:Say("自动拾取")
        end
    end
end)
AddPlayerPostInit(function(inst)
    inst:DoTaskInTime(0, function()
        if inst == ThePlayer then
            TheInput:AddKeyDownHandler(KEY_R, function()
                local screen = TheFrontEnd:GetActiveScreen()
                local IsHUDActive = screen and screen.name == "HUD"
                if inst:IsValid() and IsHUDActive and ThePlayer.HUD and not ThePlayer.HUD:HasInputFocus() then
                    SendModRPCToServer(GetModRPC('mymod', "pickitems"))
                end
            end)
        end
    end)
end)
